﻿@using DevExpress.ExpressApp
@using DevExpress.ExpressApp.Blazor
@using DevExpress.ExpressApp.Blazor.Components
@using DevExpress.ExpressApp.Blazor.Services
@using DevExpress.ExpressApp.Blazor.Templates
@using DevExpress.ExpressApp.Templates.ActionControls
@using Microsoft.Extensions.Options
@using Microsoft.JSInterop

@inherits FrameTemplateComponentBase<DesignerApplicationWindowTemplate>

@inject IActionExecutorService ActionExecutorService
@inject NavigationManager NavigationManager

<div id="main-window-template-component" class="app h-100 d-flex flex-column">
    <ComponentModelObserver ComponentModel="@FrameTemplate.NavigateBackActionControl.ButtonModel">
        <div class="header d-flex flex-row shadow-sm navbar-dark flex-nowrap @DetailViewHeaderClass @NavigateBackActionHeaderClass">
            <div class="header-right-side w-100 overflow-hidden d-flex align-items-center px-2 px-sm-3">
                @FrameTemplate.NavigateBackActionControl.GetComponentContent()
                <ViewCaptionComponent WindowCaption="@FrameTemplate" />
                <SizeModeContainer>
                    @FrameTemplate.HeaderToolbar.GetComponentContent()
                </SizeModeContainer>
            </div>
        </div>
    </ComponentModelObserver>
    <div class="xaf-flex-auto overflow-hidden d-flex">
        <div class="main xaf-flex-auto overflow-hidden d-flex flex-column">
            <SizeModeContainer>
                @if (FrameTemplate.IsActionsToolbarVisible && @FrameTemplate.Toolbar.ContainsVisibleActionControl())
                {
                    <div class="main-toolbar py-3 px-2 px-sm-3">@FrameTemplate.Toolbar.GetComponentContent()</div>
                }
                <div class="main-content xaf-flex-auto overflow-auto pb-3 px-2 px-sm-3">
                    <ViewSiteComponent View="@FrameTemplate.View" />
                </div>
            </SizeModeContainer>
        </div>
    </div>
</div>

@code {
    public static RenderFragment Create(DesignerApplicationWindowTemplate applicationWindowTemplate)
    {
        return @<DesignerApplicationWindowTemplateComponent FrameTemplate="@applicationWindowTemplate" />;
    }

    private string DetailViewHeaderClass => "xaf-detail-view-header";

    private string NavigateBackActionHeaderClass => FrameTemplate.NavigateBackActionControl.Visible ? "xaf-show-navigate-back-action" : default;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            if (!FrameTemplate.NavigateBackActionControl.Visible)
            {
                FrameTemplate.NavigateBackActionControl.ButtonModel.Visible = true;
                FrameTemplate.NavigateBackActionControl.ButtonModel.Click = EventCallback.Factory.Create<MouseEventArgs>((object)this, async () =>
                {
                    await ActionExecutorService.InvokeAsync(() =>
                    {
                        NavigationManager.NavigateTo("/");
                        return Task.CompletedTask;
                    }, null);
                });
            }
            await JSRuntime.InvokeVoidAsync("xaf.initApplicationWindowTemplate");
        }
        await base.OnAfterRenderAsync(firstRender);
    }
}
